<?php
class Product extends Eloquent {

	protected $table = 'products';
	
	/**
     * get product by id
     * @author Thuan Truong
     */
    public function getProductById ($id) {
        return $this->whereRaw('id = ? and is_deleted = ?', array($id, 0))->first();
    }
    
    /**
     * Get Media
     */
    public function media() {
        return $this->belongsTo('Media');
    }
    
    /**
     * get all product
     * @author Thuan Truong
     */
    public function getAllProducts() {
        $products = Product::where('is_deleted', '=', 0)->get();
        return $products;
    }
	
	/**
     * get all product publish
     * @author Thuan Truong
     */
    public function getAllProductsPublish() {
        $products = Product::whereRaw('is_deleted = ? and status = ?', array(0,1))->get();
        return $products;
    }
    
    
    /**
     * count all product
     * @author Thuan Truong
     */
    public function countAllProducts() {
        $products = Product::where('is_deleted', '=', 0)->count();
        return $products;
    }
    
    /**
     * Validate for create 
     * @author Thuan Truong
     * @param input
     */
     public function validate($input) {
        $rules = array(
            'name_en' => 'required|max:250',
            'name_vi' => 'required|max:250',
            'price_sale' => 'required|Integer|Min:0',
            'file' => 'required',
            'code' => 'unique:products|max:30'
        );
            
        $validator = Validator::make($input, $rules);
        return $validator;
     }

    /**
     * Validate for update
     * @author Thuan Truong
     * @param input
     */
     public function validateUpdate($input) {
        $rules = array(
            'name_en' => 'required|max:250',
            'name_vi' => 'required|max:250',
            'price_sale' => 'required|Integer|Min:0',
            'file' => 'required',
            'code' => 'max:30'
        );
            
        $validator = Validator::make($input, $rules);
        return $validator;
     }
     
     /**
      * Save product when create
      * @author Thuan Truong
      */
     public function createProduct ($input) {
         $product = new Product;
         
         $product->name_en = $input['name_en'];
         $product->name_vi = $input['name_vi'];
         
         if(empty($input['code'])) {
             $product->code = $this->getCode();
         } else {
            $product->code = $input['code'];
         }
         $product->category_id = $input['category'];
         $product->media_id = $input['file']; //check empty
         $product->madein = $input['madein'];
         $product->price_sale = $input['price_sale'];
         $product->price_original = $input['price_original'];
         $product->weight = $input['weight'];
         $product->size = $input['size'];
         $product->color = $input['color'];
         $product->material = $input['material'];
         $product->description_en = $input['description_en'];
         $product->description_vi = $input['description_vi'];
         $product->note_en = $input['note_en'];
         $product->note_vi = $input['note_vi'];
         $product->status = 0;
         $product->save();
     }
     
     /**
      * Update product when create
      * @author Thuan Truong
      */
     public function updateProduct ($input, $id) {
         $product = Product::find($id);
         
         $product->name_en = $input['name_en'];
         $product->name_vi = $input['name_vi'];
         
         if(empty($input['code'])) {
             $product->code = $this->getCode();
         } else {
            $product->code = $input['code'];
         }
         $product->category_id = $input['category'];
         $product->media_id = $input['file']; //check empty
         $product->madein = $input['madein'];
         $product->price_sale = $input['price_sale'];
         $product->price_original = $input['price_original'];
         $product->weight = $input['weight'];
         $product->size = $input['size'];
         $product->color = $input['color'];
         $product->material = $input['material'];
         $product->description_en = $input['description_en'];
         $product->description_vi = $input['description_vi'];
         $product->note_en = $input['note_en'];
         $product->note_vi = $input['note_vi'];
         $product->update();
     }
     
     /**
      * destroy product
      * @author Thuan Truong
      */
     public function deleteProduct($id) {
         $product = Product::find($id);
         if (empty($product)) {
             return Redirect::action('ProductController@index')->with('f_notice', 'Sản phẩm này không tồn tại hoặc đã bị xóa');
         }
         $product->is_deleted = 1;
         $product->update();
     }
     
     
     /**
      * Create Code (random)
      * @author Thuan Truong
      * @return string (length = 8)
      */
     public function getCode() {
         $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         $code =  substr(str_shuffle(str_repeat($pool, 8)), 0, 8);
         
         return $code;
     }
	
}
    